//
//  SNRepairAskDataManager.m
//  app
//
//  Created by 王少宁 on 15/8/31.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNRepairAskDataManager.h"

@interface SNRepairAskDataManager ()
@property (nonatomic, retain) JKHttpClient * http;
@property (nonatomic, retain) SNUniteUrl * uniteUrl;
@end

@implementation SNRepairAskDataManager

+ (instancetype)shareManager
{
    AS_SINGLETON(SNRepairAskDataManager, manager, {
        manager = [[SNRepairAskDataManager alloc] init];
        manager.http = [[JKHttpClient alloc] init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
    });
    return manager;
}

- (void)loadDataWirhResult:(SNRepairAskResult *)result mallComId:(NSString *)mall_com_id address:(NSString*)addresss
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.success = NO;
    result.failure = YES;
    
//    NSDictionary * parms = mall_com_id && mall_com_id.length ? @{@"mall_com_id":mall_com_id} : @{};
    
    NSDictionary * params = @{@"address":addresss,
                              @"mall_com_id":mall_com_id};
    
//    URL: http://ceshi.sx-soft.net/android_oa/app_get_item_by_repair_id?mall_com_id=17&address=考拉之家太原运营中心熙苑测试楼1-101张正
    
    NSString * str = [NSString stringWithFormat:@"http://mylife114.com/android_oa/app_get_item_by_repair_id?mall_com_id=%@&address=%@",mall_com_id,addresss];
    
    NSString * url = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [self.http get:url  result:^(JKResponse *resp) {
        if (resp.success) {
            result.success = YES;
            result.failure = NO;
            NSMutableArray * mutableArray = [NSMutableArray array];
            for (NSDictionary * dic in resp.content[@"result"]) {
                SNRepairTypeModel * model = [SNRepairTypeModel modelWithDict:dic];
                [mutableArray addObject:model];
            }
            result.repairTypeModelsArray = mutableArray;
        }
        [result invoke];
    }];
}



- (void)submitRepairAskWithResult:(SNSubmitRepairAskResult *)result repairAskModle:(SNRepairAskSubmitModel *)model ImageArray:(NSMutableArray*)array
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.success = NO;
    result.failure = YES;
    
    NSString * image_1;
    NSString * image_2;
    NSString * image_3;
    NSString * image_4;
    
    if (array.count == 4) {
        
        image_1 = [[array objectAtIndex:0] objectForKey:@"name"];
        image_2 = [[array objectAtIndex:1] objectForKey:@"name"];
        image_3 = [[array objectAtIndex:2] objectForKey:@"name"];
        image_4 = [[array objectAtIndex:3] objectForKey:@"name"];
        
    }
    else if (array.count == 3){
        
        image_1 = [[array objectAtIndex:0] objectForKey:@"name"];
        image_2 = [[array objectAtIndex:1] objectForKey:@"name"];
        image_3 = [[array objectAtIndex:2] objectForKey:@"name"];
    }
    else if (array.count == 2){
        image_1 = [[array objectAtIndex:0] objectForKey:@"name"];
        image_2 = [[array objectAtIndex:1] objectForKey:@"name"];
        
    }else if (array.count == 1)
    {
        image_1 = [[array objectAtIndex:0] objectForKey:@"name"];
    }else
    {
        
    }
    
    
    
    
    NSDictionary * params = @{@"mall_com_id":model.mall_com_id && model.mall_com_id.length ? model.mall_com_id : @"",
                             @"apply_type" : model.apply_type,
                             @"apply_name" : model.apply_name && model.apply_name.length ? model.apply_name : @"",
                             @"telephone" : model.telephone && model.telephone.length ? model.telephone : @"",
                             @"appointment_at" : model.appointment_at && model.appointment_at.length ? model.appointment_at : @"",
                             @"appointment_at_end" : model.appointment_at_end && model.appointment_at_end.length ? model.appointment_at_end : @"",
                             @"address" : model.address && model.address.length ? model.address : @"",
                             @"repair_item" : model.repair_item && model.repair_item.length ? model.repair_item : @"",
                             @"info" : model.info && model.info.length ? model.info : @"",
                             @"add_code" : model.add_code && model.add_code.length ? model.add_code : @"",
                              @"image_1":image_1 && image_1.length ? image_1 : @"",
                              @"image_2":image_2 && image_2.length ? image_2 : @"",
                              @"image_3":image_3 && image_3.length ? image_3 : @"",
                              @"image_4":image_4 && image_4.length ? image_4 : @"",
                              @"com_id":model.com_id && model.com_id.length ? model.com_id : @"",
                              @"root_id":model.root_id && model.root_id.length ? model.root_id : @"",
                              @"uid":model.uid && model.uid.length ? model.uid : @"",
                              @"repair_item_id":model.repair_item_id && model.repair_item_id.length ? model.repair_item_id : @""
                             };
    
    NSString * url = @"http://mylife114.com/android_oa/app_repair_apply";
    
    [self.http post:url params:params result:^(JKResponse *resp) {

        if (resp.status == JKResponseOK) {
            result.success = YES;
            result.failure = NO;
        } else {
            result.message = resp.message;
        }
        [result invoke];
    }];
    
}


-(void)subPicWihtResult:(SNSubmitPictureResult *)result Params:(NSDictionary *)params Sign:(NSInteger)sign
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.success = NO;
    result.failure = YES;

    
    NSString * url = @"http://mylife114.com/android_oa/repair_image_upload";
    
    [self.http post:url params:params result:^(JKResponse *resp) {
        
        result.sign = sign;
        
        if (resp.status == JKResponseOK) {
            result.success = YES;
            result.failure = NO;
        } else {
            result.message = resp.message;
        }
        [result invoke];
    }];
    
}


@end
